// 2025/10/18
// 分隔链表

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* partition(ListNode* head, int x) {
        ListNode small, *stail = &small;
        ListNode big, *btail = &big;
        ListNode* cur = head;
        while(cur)
        {
            if(cur->val < x)
            {
                stail->next = cur;
                stail = stail->next;
            }
            else
            {
                btail->next = cur;
                btail = btail->next;
            }
            cur = cur->next;
        }
        stail->next = big.next;
        btail->next = nullptr;
        return small.next;
    }
};